class UsersController < ApplicationController
  before_filter :isCompanyOwner, :expect => []
  before_filter :isNotCompanyOwner, :only => []
  def show
    # Id der geöffneten Company
    @cid = current_user.company_pid 
    
    # Instanzvariable für alle nicht zugeordneten Nutzer der Company 
    @user = User.where( :company_pid => @cid )  
    
    calculateUserLoad
    
    # hier alle aktiven projekte der company auswählen
    @project = Project.where(:company_id => @cid) 
  end
  
  def calculateUserLoad
    @cid = current_user.company_pid 
    @allusers = User.where('company_pid' => @cid)
    @underloadHash = Hash.new
    @overloadHash = Hash.new
    
    @allusers.each do |user|
      @entries = ProjectToUser.where('user_id'=> user.id)
      sum = 0
      @entries.each do |entry|
        sum = sum + entry.percentage_id
      end
      sum = sum * 10
      if sum < 100
        @underloadHash[user.id] = sum
      end
      if sum > 100
        @overloadHash[user.id] = sum
      end
    end
  end

  def new
    @user = User.new
    @cid = current_user.company_pid 
    @url = "create"
    render :layout => nil
  end

  def create
    @paras = params[:items].split(",");
    
    o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
    password = (0...10).map{ o[rand(o.length)] }.join
    
    @user = User.new
    @user.name = @paras[0]
    @user.email = @paras[1]
    @user.company_pid = current_user.company_pid 
    @user.password = password
    @user.password_confirmation = password
    
    if @user.save
      render :text => @user.id
      Signup.new_user(@user,password).deliver
    else
      render :text => 0         
    end
  end

  def edit 
    @user = User.find_by_id(params[:id])
    @url = "update"  
    render :layout => nil
  end
  
  def update
    @paras = params[:items].split(",")
    user_id = @paras[0].split("_")[1]
    @user = User.find_by_id(user_id)
    @user.name = @paras[1]
    @user.email = @paras[2]
    if @user.save
      render :text => 1
    else
      render :text => 0 
    end
  end
  
  def show_me
    @image_url = ""
    @url = ""
    p = Painting.where(:user_id => current_user.id)
    if p.nil?
       @image_url = p.first.image_url 
    end
    
    if p.count == 0
      @url = "../paintings/create"
    else
      @url = "../paintings/update"
    end
    
    if params[:layout] == 1
       render :layout => nil
    end
  end
  
  def change_password
  end
  
  def update_password
    if current_user.update_attributes(:password => params[:password],    :password_confirmation => params[:password_confirmation])
      render :text => "0", :layout => nil
    else
      @src = "" 
      current_user.errors.full_messages.each{|msg| @src = @src + "- " + msg + "<br>" };
      render :text => @src, :layout => nil
    end  
  end
  
  def change_profile
    @image_url = ""
    @url = ""
    p = Painting.where(:user_id => current_user.id)
    if p.nil?
       @image_url = p.image_url 
    end
    
    if p.count == 0
      @url = "../paintings/create"
    else
      @url = "../paintings/update"
    end
    render :layout => nil
  end
  
  def update_profile
    current_user.update_attribute(:name , params[:name])
    current_user.update_attribute(:email , params[:email])

    render :text => "0", :layout => nil
  end

  def delete
    @paras = params[:items].split(",")
    undeleted_users = "" # shall be empty!
    i = 0
    num = @paras.count
    while i<num
      user = User.find_by_id(@paras[i])
      user.destroy   
      if !user.destroyed?
        undeleted_users = undeleted_users + ","
      end 
      i = i + 1
    end
    
    if undeleted_users.length > 1
      undeleted_users = undeleted_users[0..undeleted_users.length-2]
    else
      undeleted_users = 0
    end
    render :text => undeleted_users
  end
end
